package com.travel.server.manager.impl.command.customer;

import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dto.request.EmptyRequest;
import com.travel.server.manager.dto.response.customer.QueryTagListResponse;
import com.travel.server.manager.dto.response.customer.QueryTagListResponse.TagItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.PageDto;
import com.travel.server.manager.impl.dal.TagDal;
import com.travel.server.manager.impl.dal.TagDal.TagParam;
import com.travel.server.manager.model.TagInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018\11\10 0010 17:17
 * @Desc:
 */
@Component
@Slf4j
public class QueryTagListCommand extends AbstractCommand<EmptyRequest, QueryTagListResponse> {

    @Autowired
    private TagDal tagDal;

    @Override
    protected void checkInput(EmptyRequest request) {
    }

    @Override
    public QueryTagListResponse handle(EmptyRequest request) {
        TagParam param = new TagParam();
        PageDto<TagInfo> pageDto = tagDal.select(param);

        return makeResponse(pageDto);
    }

    public QueryTagListResponse makeResponse(PageDto<TagInfo> pageDto){
        QueryTagListResponse response = new QueryTagListResponse();
        response.setTotalNum(pageDto.getTotal());
        List<TagItem> tagList = new ArrayList<>();

        List<TagInfo> list = pageDto.getList();
        if(!CollectionUtil.isEmpty(list)){
            for(TagInfo tagInfo : list){
                TagItem item = new TagItem();
                item.setId(tagInfo.getId());
                item.setTagName(tagInfo.getName());
                item.setTagExplains(tagInfo.getExplains());
                tagList.add(item);
            }
        }
        response.setTagList(tagList);

        return response;
    }
}
